package com.rh.gtoa.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rh.gtoa.common.model.Result;
import com.rh.gtoa.common.service.RedisService;
import com.rh.gtoa.mapper.UmsDepaMapper;
import com.rh.gtoa.mapper.UmsUserMapper;
import com.rh.gtoa.model.UmsDepa;
import com.rh.gtoa.model.UmsRole;
import com.rh.gtoa.model.UmsUser;
import com.rh.gtoa.model.UmsUserRole;
import com.rh.gtoa.service.IUmsRoleService;
import com.rh.gtoa.service.IUmsUserRoleService;
import com.rh.gtoa.service.IUmsUserService;
import com.rh.gtoa.vo.UmsPermissionVO;
import com.rh.gtoa.vo.UmsUserVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 用户表
 *
 * @author hqb
 * @date 2020-09-12 16:31:21
 */
@Slf4j
@Service
public class UmsUserServiceImpl extends ServiceImpl<UmsUserMapper, UmsUser> implements IUmsUserService {
    @Resource
    private UmsUserMapper umsUserMapper;
    @Autowired
    private IUmsUserService umsUserService;
    @Autowired
    private RedisService redisService;
    @Resource
    private UmsDepaMapper umsDepaMapper;
    @Autowired
    private IUmsUserRoleService umsUserRoleService;
    @Autowired
    private IUmsRoleService umsRoleService;
    /**
     * 列表
     * @param params
     * @return
     */
    public Page<UmsUser> findList(Map<String, Object> params){
        Integer pageNum = MapUtils.getInteger(params, "pageNum");
        Integer pageSize = MapUtils.getInteger(params, "pageSize");
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = -1;
        }
        Page<UmsUser> pages = new Page<>(pageNum, pageSize);
        return umsUserMapper.findList(pages, params);
    }

    @Override
    public Result login(UmsUser umsUser) {
        String userAccount = umsUser.getUserAccount();
        String userPwd = umsUser.getUserPwd();
        UmsUser existUmsUser = umsUserService.getOne(new QueryWrapper<UmsUser>()
                .eq("user_account", userAccount)
                .eq("user_pwd", userPwd)
        );
        if (existUmsUser!=null){
            redisService.set(SecureUtil.md5(existUmsUser.getUserAccount()), JSONObject.toJSONString(existUmsUser),60*60*24);
            UmsUserVO umsUserVO=new UmsUserVO();
            BeanUtil.copyProperties(existUmsUser,umsUserVO);
            UmsDepa umsDepa = umsDepaMapper.selectOne(new QueryWrapper<UmsDepa>()
                    .eq("code", umsUserVO.getDepaCode())
                    .eq("is_del",0));
            umsUserVO.setToken(SecureUtil.md5(existUmsUser.getUserAccount()));
            if (umsDepa != null) {
                umsUserVO.setRemark(umsDepa.getRemark());
            }
            Long id = existUmsUser.getId();
            String roleName="";
            List<UmsUserRole> umsUserRoles = umsUserRoleService.list(new QueryWrapper<UmsUserRole>().eq("user_id", id));
            if (umsUserRoles.size()>0){
                Long roleId = umsUserRoles.get(0).getRoleId();
                UmsRole umsRole = umsRoleService.getById(roleId);
                roleName= umsRole.getRoleName();
            }
            List<UmsPermissionVO> umsPermissionVOS=umsUserMapper.selectPermissionOfUser(id);
            if (umsPermissionVOS.size()<=0){
                return Result.failed(umsUserVO,"无用户权限，请联系管理员分配角色");
            }
            List list=(List) umsPermissionVOS.stream().distinct().collect(Collectors.toList());
            umsUserVO.setRoleName(roleName);
            umsUserVO.setUmsPermissionVOS(list);
            return Result.succeed(umsUserVO,"登录成功");
        }
        return Result.failed("登录失败，账号或密码错误");
    }

    @Override
    public int updateToNull(Long id) {
        return umsUserMapper.updateToNull(id);
    }

    @Override
    public Result logout(UmsUser umsUser) {
        String userAccount = umsUser.getUserAccount();
        Object o = redisService.get(SecureUtil.md5(userAccount));
        if (o!=null){
            redisService.del(SecureUtil.md5(userAccount));
        }
        return Result.succeed("退出成功");
    }

    @Override
    public List<UmsUser> getManager(Map<String, Object> params) {
        return umsUserMapper.getManager(params);
    }
}
